package org.example.lotterysystem.Test;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class RabbitProducer {
    public static void main(String[] args) throws Exception {
        // 1. 创建连接⼯⼚
        ConnectionFactory factory = new ConnectionFactory();
        //2. 设置参数
        factory.setHost("127.0.0.1");//ip 默认值localhost
        factory.setPort(5672); //默认值5672
        factory.setVirtualHost("Quenue");//虚拟机名称, 默认 /
        factory.setUsername("guest");//⽤⼾名,默认guest
        factory.setPassword("guest");//密码, 默认guest
        //3. 创建连接Connection
        Connection connection = factory.newConnection();
        //4. 创建channel通道
        Channel channel = connection.createChannel();
        //5. 声明队列
 /*
 queueDeclare(String queue, boolean durable, boolean exclusive,
boolean autoDelete, Map<String, Object> arguments)
 1.queue: 队列名称
 2.durable: 是否持久化, 当mq重启之后, 消息还在
 3.exclusive:
 * 是否独占, 只能有⼀个消费者监听队列
 * 当Connection关闭时, 是否删除队列
 4.autoDelete: 是否⾃动删除, 当没有Consumer时, ⾃动删除掉
 5.arguments: ⼀些参数
        //如果没有⼀个hello 这样的⼀个队列, 会⾃动创建, 如果有, 则不创建
        channel.queueDeclare("hello", true, false, false, null);
        //6. 通过channel发送消息到队列中
 /*
 basicPublish(String exchange, String routingKey, AMQP.BasicProperties
props, byte[] body)
 1. exchange: 交换机名称, 简单模式下, 交换机会使⽤默认的""
 2.routingKey: 路由名称, routingKey = 队列名称
 3.props: 配置信息
 4.body: 发送消息的数据
 */
        String msg = "Hello World";
        //使⽤的是内置交换机. 使⽤内置交换机时, routingKey要和队列名称⼀样, 才可以路由
        channel.basicPublish("", "hello", null, msg.getBytes());
        //7.释放资源
        System.out.println(msg + "消息发送成功");
        channel.close();
        connection.close();
    }
}
